package core.cards.election.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import core.cards.IFormBackend;
import core.utils.Converter;

public class ElectionBackend implements IFormBackend, Serializable {

	private static final long serialVersionUID = -5749680111392954451L;

	private ArrayList<VotingCard> votingCardList;

	public static ElectionBackend createDemoElectionFormBackend() {
		ElectionBackend electionFormBackend = new ElectionBackend();

		VotingCard vc1 = new VotingCard("Wybory prezydenckie", "Kandydaci na Prezydenta Rzeczypospolitej Polskiej", 1, electionFormBackend);

		VotingCardOption vco1 = new VotingCardOption("JUREK Marek", vc1);
		VotingCardOption vco2 = new VotingCardOption("KACZYŃSKI Jarosław Aleksander", vc1);
		VotingCardOption vco3 = new VotingCardOption("KOMOROWSKI Bronisław Maria", vc1);
		VotingCardOption vco4 = new VotingCardOption("KORWIN-MIKKE Janusz Ryszard", vc1);
		VotingCardOption vco5 = new VotingCardOption("LEPPER Andrzej Zbigniew", vc1);
		VotingCardOption vco6 = new VotingCardOption("MORAWIECKI Kornel Andrzej", vc1);
		VotingCardOption vco7 = new VotingCardOption("NAPIERALSKI Grzegorz Bernard", vc1);
		VotingCardOption vco8 = new VotingCardOption("OLECHOWSKI Andrzej Marian", vc1);
		VotingCardOption vco9 = new VotingCardOption("PAWLAK Waldemar", vc1);
		VotingCardOption vco10 = new VotingCardOption("ZIĘTEK Bogusław Zbigniew", vc1);

		vc1.addVotingCardOption(vco1);
		vc1.addVotingCardOption(vco2);
		vc1.addVotingCardOption(vco3);
		vc1.addVotingCardOption(vco4);
		vc1.addVotingCardOption(vco5);
		vc1.addVotingCardOption(vco6);
		vc1.addVotingCardOption(vco7);
		vc1.addVotingCardOption(vco8);
		vc1.addVotingCardOption(vco9);
		vc1.addVotingCardOption(vco10);

		electionFormBackend.addVotingCard(vc1);

		return electionFormBackend;
	}

	public ElectionBackend() {
		votingCardList = new ArrayList<>();
	}

	public ArrayList<VotingCard> getVotingCardList() {
		return votingCardList;
	}

	public String getVoteInXML() {
		return Converter.objectToXML(votingCardList);
	}

	public void addVotingCard(VotingCard votingCard) {
		votingCardList.add(votingCard);
	}

	public void removeVotingCard(VotingCard votingCard) {
		votingCardList.remove(votingCard);
	}

	@Override
	public boolean isValid() {
		if (votingCardList.isEmpty()) {
			return false;
		}
		for (VotingCard votingCard : votingCardList) {
			List<VotingCardOption> votingCardOptionList = votingCard.getVotingCardOptionList();
			if (votingCardOptionList.isEmpty()) {
				return false;
			}
		}
		return true;
	}

	@Override
	public String getValidationMessage() {
		if (votingCardList.isEmpty()) {
			return "There has to be at least one Voting Card";
		}
		for (VotingCard votingCard : votingCardList) {
			List<VotingCardOption> votingCardOptionList = votingCard.getVotingCardOptionList();
			if (votingCardOptionList.isEmpty()) {
				return "Each Voting Card has to have at least one Voting Card Option";
			}
		}
		return null;
	}

	@Override
	public void clean() {
		for (VotingCard votingCard : votingCardList) {
			for (VotingCardOption votingCardOption : votingCard.getVotingCardOptionList()) {
				if (votingCardOption.isChecked()) {
					votingCardOption.check();
				}
			}
		}
	}

	@Override
	public String toString() {
		String output = "";

		output += "backend:\n";

		int votingCardIdx = 0;
		for (VotingCard votingCard : votingCardList) {
			output += "\t" + votingCardIdx++ + ": " + votingCard.getName() + "\n";
			int votingCardOptionIdx = 0;
			for (VotingCardOption votingCardOption : votingCard.getVotingCardOptionList()) {
				output += "\t\t" + votingCardOptionIdx++ + ": " + votingCardOption + "\n";
			}
		}

		return output;
	}
}
